<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 系统参数管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Single extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'single_page';
		$data['perm'] = 'single';
		$condition = array();
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['title'] = $this->input->post('title');
			$data['keywords'] = $this->input->post('keywords');
			$data['description'] = $this->input->post('description');
			$data['likeid'] = $this->input->post('likeid');
			$data['ismake'] = $this->input->post('ismake');
			$data['content'] = $this->input->post('content');
			$data['upTimeStart'] = $this->input->post('upTimeStart');
			$data['upTimeEnd'] = $this->input->post('upTimeEnd');
			$data['createTimeStart'] = $this->input->post('createTimeStart');
			$data['createTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['title'] = $this->input->get('title');
			$data['keywords'] = $this->input->get('keywords');
			$data['description'] = $this->input->get('description');
			$data['likeid'] = $this->input->get('likeid');
			$data['ismake'] = $this->input->get('ismake');
			$data['content'] = $this->input->get('content');
			$data['upTimeStart'] = $this->input->get('upTimeStart');
			$data['upTimeEnd'] = $this->input->get('upTimeEnd');
			$data['createTimeStart'] = $this->input->get('createTimeStart');
			$data['createTimeEnd'] = $this->input->get('createTimeEnd');
		}

		$data['title'] = urldecode($data['title']);
		$data['likeid'] = urldecode($data['likeid']);

		$condition = array();
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled'])>0){
			$condition['disabled'] = $data['disabled'];
		}
		
		if(strlen($data['ismake'])>0){
			$condition['ismake'] = $data['ismake'];
		}
		
		if(strlen($data['title'])>0){
			array_push($db_like_key,'upper(title)');
			array_push($db_like_value,strtoupper($data['title']));
		}
		if(strlen($data['likeid'])>0){
			array_push($db_like_key,'upper(likeid)');
			array_push($db_like_value,strtoupper($data['likeid']));
		}
		if(strlen($data['upTimeStart'])>0){
			$condition['uptime >= '] = strtotime($data['upTimeStart']);
		}
		if(strlen($data['upTimeEnd'])>0){
			$condition['uptime <= '] = strtotime($data['upTimeEnd']) + 24*60*60;
		}
		if(strlen($data['createTimeStart'])>0){
			$condition['createtime >= '] = strtotime($data['createTimeStart']);
		}
		if(strlen($data['createTimeEnd'])>0){
			$condition['createtime <= '] = strtotime($data['createTimeEnd']) + 24*60*60;
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}

		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;
		
		if($this->input->server('REQUEST_METHOD')=='POST'){
			//print_r($where);
			//exit;
			if(strtoupper($active)=='DEL'){
				if(strlen($del)==0){
					$condition['db_where_in'] = array('id',$id);
				}				
				$operationData = array(
					'adminId'=>$this->session->userdata('adminUserId'),
					'title'=>'删除单页文档',
					'url'=>$this->input->server('REQUEST_URI'),
					'remark'=>'删除单页文档，删除内容：'.json_encode($this->JayphaDB->getSingleDB($this->dbTablePrefix.$tvName,$condition))
				);
				$this->db->trans_start();
				$this->JayphaDB->setAdminOperation($operationData);
				$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$condition);
				$this->db->trans_complete();
			}
		}

		unset($condition);

		$config['base_url'] = site_url("admin/single/index/");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';

		$this->pagination->initialize($config);

		$data['currentSite'] = '频道管理<span class="split">>></span>单页文档管理<span class="split">>></span>文档列表';
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = '单页文档管理';
		$data['editPage'] = site_url('admin/single/edit');
		$data['editTitle'] = '修改文档';
		$data['addTitle'] = '添加文档';
		$data['perm'] = 'single';
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'id';
		$num = $this->uri->segment(4,0);
		$data['singleList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		$this->load->view("admin/$this->adminCurrentTemplate/single",$data);		
	}
	
	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'single_page';
		$data['tvName'] = $tvName;
		$data['perm'] = 'single';
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$id = $this->input->post('id');
			$disabled = $this->input->post('disabled');
			$ismake = $this->input->post('ismake');
			$title = $this->input->post('title');
			$keywords = $this->input->post('keywords');
			$description = $this->input->post('description');
			$template = $this->input->post('template');
			$likeid = $this->input->post('likeid');
			$content = $this->input->post('content');
			if(strlen($id)>0){
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$id,'upper(title)'=>strtoupper($title)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'修改失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，此文档标题已经存在，请选用其它标题！</span>',
							'url'=>'history.back()',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$where = array('id'=>$id);
				$updateData = array(
						'disabled'=>$disabled,
						'ismake'=>$ismake,
						'title'=>$title,
						'keywords'=>$keywords,
						'description'=>$description,
						'template'=>$template,
						'likeid'=>$likeid,
						'uptime'=>now(),
						'content'=>$content
				);
				$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$where);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>220,
						'height'=>108,
						'title'=>'修改成功 ',
						'content'=>'您的文档已成功修改！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}else{
				$addData = array(
						'disabled'=>$disabled,
						'ismake'=>$ismake,
						'title'=>$title,
						'keywords'=>$keywords,
						'description'=>$description,
						'template'=>$template,
						'likeid'=>$likeid,
						'uptime'=>now(),
						'createtime'=>now(),
						'content'=>$content
				);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('upper(title)'=>strtoupper($title)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'添加失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，您预添加的文档已经存在，请取其它文档标题！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$addData,true);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>180,
						'height'=>108,
						'title'=>'添加成功 ',
						'content'=>'您的文档已成功添加！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])>0){
				foreach($this->JayphaDB->getDB('','','',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'id',false) as $item){					
					$data['disabled'] = $item->disabled;
					$data['ismake'] = $item->ismake;
					$data['title'] = $item->title;
					$data['keywords'] = $item->keywords;
					$data['description'] = $item->description;
					$data['likeid'] = $item->likeid;
					$data['template'] = $item->template;
					$data['content'] = $item->content;
				}
			}else{
				$data['disabled'] = false;
				$data['ismake'] = false;
				$data['title'] = '';
				$data['keywords'] = '';
				$data['description'] = '';
				$data['likeid'] = '';
				$data['template'] = '';
				$data['content'] = '';
			}
			$this->CKEditor = new CKEditor();
			$this->CKEditor->BasePath = base_url().$this->config->item('CKEditor_path');
			$this->CKEditor->config['height'] = 126;
			$this->CKEditor->config['toolbar'] = 'full';
			$this->CKEditor->config['language'] = 'zh-cn';
			$this->CKEditor->config['toolbarStartupExpanded'] = true;
			CKFinder::SetupCKEditor($this->CKEditor,$this->config->item('CKFinder_path'));
			
			$this->load->view("admin/$this->adminCurrentTemplate/singleEdit",$data);
		}		
	}
}
/* End of file single.php */
/* Location: ./application/controllers/admin/single.php */